<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <div>
        <p>项目名称: {{project_name}}</p>
        <p>模块名称: {{project_task_name}}</p>
        <br>
      </div>
      <shopee-table class=HistoryListData stripe :data="HistoryListData" >
        <shopee-table-column prop="task_name" label="任务名称" :width="200" fixed="left">
        </shopee-table-column>

        <shopee-table-column prop="executor_person" label="执行人" :width="200">
        </shopee-table-column>

        <shopee-table-column prop="pfb" label="PFB" :width="400">
          <template slot-scope="props">
              <div v-if="props.row.pfb === ''">默认分支</div>
              <div v-else>
                <h style="white-space: pre-wrap;" v-html="change(props.row.pfb)"></h>
              </div>
          </template>
        </shopee-table-column>

        <shopee-table-column prop="env" label="运行环境" :width="100">
        </shopee-table-column>

        <shopee-table-column prop="passed_num" label="通过数/总数" :width="150">
          <template slot-scope="props">
              {{props.row.passed_num}}/{{props.row.case_num}}
          </template>
        </shopee-table-column>

        <shopee-table-column prop="passed_num" label="通过率" :width="100">
          <template slot-scope="props">
            <div v-if="calc(props.row) === '100.00%'"><span style="color:green;">{{calc(props.row)}}</span></div>
            <div v-else><span style="color:red;">{{calc(props.row)}}</span></div>
          </template>
        </shopee-table-column>

        <shopee-table-column prop="result_time" label="运行时间" :width="200">
        </shopee-table-column>

        <shopee-table-column
          label="详情链接" :width="125">
          <template slot-scope="{row}">
              <div slot="reference">
                      <el-link target="_blank" :href="'https://www.shopee-qa.com/api/#/result_detail/'+row.task" type="primary">查看报告</el-link>
              </div>
          </template>
        </shopee-table-column>

        <shopee-table-column label="操作" :width="120" fixed="right">
            <template slot-scope="props">
                <div v-if="props.row.is_finish === '0'">
                  <el-button size="small" @click="stopTask(props.row.task_id)" type="primary" outline>点击中止活动</el-button>
                </div>
                <div v-else-if="props.row.is_finish === '1'">
                  <el-button size="small" type="primary" outline disabled>任务已完成</el-button>
                </div>
                <div v-else>
                  <el-button size="small" type="primary" outline disabled>任务已停止</el-button>
                </div>
            </template>
        </shopee-table-column>
      </shopee-table>
      <br>
      <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5,10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalRow">
      </el-pagination>
    </div>
  </el-card>
</template>

  <script>
    import {api, getRequest, postRequest} from "@/api/commit";
    export default {
      name : "HistoryListTable",
      components: {
      },
      data() {
        return {
          project_name:"",
          project_task_name:"",
          currentPage: 1,
          totalRow: 0,
          pageSize: 10 ,
          HistoryListData: []
        }
      },
      methods: {
        stopTask(stop_task_id) {
          let params = {}
          params.task_id=stop_task_id
          postRequest(api['stop_task'], params).then((res)=>{
            this.init_data()
          },(error)=>{
            this.resultReturn = "请检查您的输入是不是有问题&依赖方报错俺也没办法"
          })
        },  
        calc(row){
          return (row.passed_num/row.case_num*100).toFixed(2)+"%"
        },
        change:function(content){
          content = content.replace(/;/g,";</br>");
          content = content.replace(/；/g,"；</br>");
          return content;
        },
        handleSizeChange(val) {
          this.pageSize = val;
          this.init_data();
        },
        handleCurrentChange(val) {
          this.currentPage = val;
          this.init_data();
        },
        async init_data() {
          let params = {
            "template_task_id": this.$route.query.template_task_id,
            "page_size":this.pageSize,
            "page":this.currentPage
          }
          this.project_name = this.$route.query.project_name
          this.project_task_name = this.$route.query.project_task_name
          await getRequest(api['sycn_result']);
          const res = await getRequest(api['task_history'],params);
          const { data, total} = res.resultReturn
          this.HistoryListData= data
          this.totalRow= total
        },
      },
      created () {
        this.init_data()
      }
    }
  </script>
